package com.oocl.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.oocl.entity.Clients;
import com.oocl.entity.DeliverInfo;
import com.oocl.entity.Foods;
import com.oocl.entity.Merchants;
import com.oocl.entity.OrderComment;
import com.oocl.entity.SessionInfo;
import com.oocl.service.CartService;

@Controller
public class CartController {
	@Resource(name="cartService")
	private CartService cartService;
	
	@ResponseBody
	@RequestMapping(value="/plusCount")
	public List<SessionInfo> addToCart(String icnum,String fid,HttpSession sen){
//		模拟登陆cid
//		String cid = (String) sen.getAttribute("cid");
//		if(null==cid){
//			sen.setAttribute("cid", "4028b8815de011d8015de011de7b0000");
//		}
		
		String cid = (String) sen.getAttribute("cid");
		if(null==cid){
			return null;
		}
		
		
		String icNum = (String) sen.getAttribute("icnum");
		List<SessionInfo> cart = (List<SessionInfo>) sen.getAttribute("cart");
		if(icNum == null){
			sen.setAttribute("icnum", icnum);
			cart = new ArrayList<SessionInfo>(); 
		}
		if(cart==null){
			cart = new ArrayList<SessionInfo>(); 
		}
		//代表换商家了
		if(!icnum.equals(icNum)){
			sen.setAttribute("icnum", icnum);
			cart = new ArrayList<SessionInfo>();
		}
		//判断是否已存在同一件商品
		boolean isExit = false;
		for(SessionInfo seInfo:cart){
			if(seInfo.getFood().getFid().equals(fid)){
				seInfo.setCount(seInfo.getCount()+1);
				isExit = true;
				break;
			}
		}
		if(!isExit){
			Foods food = cartService.findFoodByFid(fid);
			SessionInfo sessionInfo = new SessionInfo();
			sessionInfo.setFood(food);
			sessionInfo.setCount(1);
			cart.add(sessionInfo);
		}
		sen.setAttribute("cart", cart);
		
		return cart;
	}
	
	@ResponseBody
	@RequestMapping(value="/updateCount")
	public List<SessionInfo> updateCount(String fid,String type,HttpSession sen){
		List<SessionInfo> cart = (List<SessionInfo>) sen.getAttribute("cart");
		//type=1表示增加  type=2表示减少
		for(SessionInfo seInfo:cart){
			if(seInfo.getFood().getFid().equals(fid)){
				if("1".equals(type)){
					seInfo.setCount(seInfo.getCount()-1);
					break;
				}else if("2".equals(type)){
					seInfo.setCount(seInfo.getCount()+1);
					break;
				}
				
			}
		}
		sen.setAttribute("cart",cart);
		return cart;
	}
	@ResponseBody
	@RequestMapping(value="/deleteItem")
	public List<SessionInfo> deleteItem(String fid,HttpSession sen){
		List<SessionInfo> cart = (List<SessionInfo>) sen.getAttribute("cart");
		for(int i =  0;i<cart.size();i++){
			if(cart.get(i).getFood().getFid().equals(fid)){
				cart.remove(i);
			}
		}
		sen.setAttribute("cart",cart);
		return cart;
	}
//	@ResponseBody
//	@RequestMapping(value="/findFoodsByIcnumAndType")
//	public List<Foods> findFoodsByIcnumAndType(String icnum,String type){
//		List<Foods> foodList = cartService.findFoodsByIcnumAndType(icnum, type);
//		return foodList;
//	}
	@ResponseBody
	@RequestMapping(value="/findFoodsByIcnumAndType/{icnum}",method={RequestMethod.GET})
	public List<Foods> findFoodsByIcnumAndType(@PathVariable String icnum,String type){
		List<Foods> foodList = cartService.findFoodsByIcnumAndType(icnum, type);
		return foodList;
	}
	
	@ResponseBody
	@RequestMapping(value="/findFoodsByInputText")
	public List<Foods> findFoodsByInputText(String text,String icnum,HttpSession sen){
		List<Foods> foodList = null;
		if("".equals(text)){
			foodList =cartService.findFoodsByIcnum(icnum);
			return foodList;
		}
		foodList = cartService.findFoodsByFname(text,icnum);
		return foodList;
	}
	
	@RequestMapping(value="/toOrderPage",method={RequestMethod.GET})
	public String toOrderPage(String icnum,String priceTotal,Model model,HttpSession sen){
		String cid = (String) sen.getAttribute("cid");
		String icNum = (String) sen.getAttribute("icnum");
		List<SessionInfo> cart = (List<SessionInfo>) sen.getAttribute("cart");
		Clients client = cartService.findClientsByCid(cid);
		Merchants merchant = cartService.findMerchantsByIcnum(icNum);
		List<DeliverInfo> addressList = cartService.findDeliverInfoBy(cid);
		model.addAttribute("infolist",cart);
		model.addAttribute("sum",priceTotal);
		model.addAttribute("client",client);
		model.addAttribute("merchant",merchant);
		model.addAttribute("addrlist",addressList);
		return "payorder";
	}
	//刚进入页面的时候初始化购物车
	@ResponseBody
	@RequestMapping(value="/findAllFoodInit")
	public List<SessionInfo> findAllFood(HttpSession sen){
		List<SessionInfo> cart = (List<SessionInfo>) sen.getAttribute("cart");
		return cart;
	}
	
	
	@ResponseBody
	@RequestMapping(value="/findMerchantCommentList/{icnum}",method={RequestMethod.GET})
	public List<OrderComment> findOrderCommentListByIcnum(@PathVariable String icnum){
		List<OrderComment> commentList = cartService.findOrderCommentListByIcnum(icnum);
		return commentList;
	}
	
}
